<?php
/**
 * Created by PhpStorm.
 * Date: 2018/11/29
 * Time: 下午2:41
 */

namespace app\api\service\payment\F4stpay;


class HTTP
{

    /**
     * 发送请求
     * @param $url
     * @param $method
     * @param string $data
     * @param array $options
     * @return mixed
     */
    public static function __request($url, $method, $data = '', $options = array())
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, true);
        // 设置Post参数
        if ($method === "POST") {
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($ch, CURLOPT_TIMEOUT, 30); //设置cURL允许执行的最长秒数
        if (!empty($options)) {
            curl_setopt_array($ch, $options);
        }
        //https请求 不验证证书和host
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        $output = curl_exec($ch);
        $response = array();
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
        $header_text = substr($output, 0, $header_size);
        $body = substr($output, $header_size);
        $headers = array();
        foreach (explode("\r\n", $header_text) as $i => $line) {
            if (!empty($line)) {
                if ($i === 0) {
                    $headers["Http-Code"] = $line;
                } else if (strpos($line, ": ")) {
                    list ($key, $value) = explode(': ', $line);
                    $headers[$key] = $value;
                }
            }
        }
        $response['headers'] = $headers;
        $response['body'] = $body;
        $response['http_code'] = $httpCode;
        curl_close($ch);
        return $response;
    }

    public static function get_headers_from_curl_response($response)
    {
        $headers = array();
        $header_text = substr($response, 0, strpos($response, "\r\n\r\n"));
        foreach (explode("\r\n", $header_text) as $i => $line)
            if ($i === 0)
                $headers['http_code'] = $line;
            else {
                list ($key, $value) = explode(': ', $line);
                $headers[$key] = $value;
            }
        return $headers;
    }

    /**
     * 发送get请求
     * @param $url
     * @param string $postData
     * @return mixed
     * @throws F4stPayHTTPException
     */
    public static function post($url, $postData = '')
    {
        if (is_array($postData)) {
            $postData = http_build_query($postData);
        }
        $res = self::__request($url, "POST", $postData);
        if ($res["http_code"] != 200) {
            throw new F4stPayHTTPException("http code is {$res['http_code']}");
        }
        return $res;
    }

    /**
     * 发送GET请求
     * @param $url
     * @param string $params
     * @return mixed
     * @throws F4stPayHTTPException
     */
    public static function get($url, $params = '')
    {
        if (is_array($params)) {
            $params = http_build_query($params);
        }
        $res = self::__request($url . "?" . $params, "GET");
        if ($res["http_code"] != 200) {
            throw new F4stPayHTTPException("request error , http code is {$res['http_code']}");
        }
        return $res;
    }
}